package com.mjk.common.ai.service;


import com.mjk.common.ai.dto.KbStarInfoResp;
import com.mjk.common.ai.entity.KnowledgeBaseStar;
import com.mjk.common.ai.util.MPPageUtil;
import com.mjk.common.core.jdbc.bean.BeePage;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mjk.common.ai.entity.User;

@Slf4j
@Service
public class KnowledgeBaseStarService {

    @Autowired
    private BeeDBService beeDBService;

    public boolean isStarred(Long userId, String kbUuid) {
        BeeQueryWrapper wrapper = BeeQueryWrapper.getWrapper(KnowledgeBaseStar.class)
                .eq(KnowledgeBaseStar::getUserId, userId)
                .eq(KnowledgeBaseStar::getKbUuid, kbUuid)
                .eq(KnowledgeBaseStar::getIsDeleted, false);
        return beeDBService.countByWrapper(wrapper) > 0;
    }

    public KnowledgeBaseStar getRecord(long userId, String kbUuid) {
        return BeeQueryWrapper.getWrapper(KnowledgeBaseStar.class)
                .eq(KnowledgeBaseStar::getUserId, userId)
                .eq(KnowledgeBaseStar::getKbUuid, kbUuid)
                .one();
    }

    public BeePage<KbStarInfoResp> listStarInfo(User user, int currentPage, int pageSize) {
        BeeQueryWrapper wrapper = new BeeQueryWrapper(KnowledgeBaseStar.class);
        wrapper.eq(KnowledgeBaseStar::getIsDeleted, false);
        wrapper.eq(KnowledgeBaseStar::getUserId, user.getId());
        wrapper.orderByDesc(KnowledgeBaseStar::getId);
        wrapper.setPage(currentPage, pageSize);
        BeePage<KnowledgeBaseStar> list = beeDBService.findManyByWrapperWithPage(wrapper);

        BeePage<KbStarInfoResp> result = new BeePage<>();
        return MPPageUtil.convertToPage(list, result, KbStarInfoResp.class);
    }

}
